//
//  AppTools.m
//  豆瓣YHY
//
//  Created by dllo on 15/10/27.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "AppTools.h"
#import "AFNetworking.h"
@implementation AppTools


+ (void)getLocalDataWithURL:(NSString *)AFH andBlock:(AppToolsBlock)appBlock{
   // NSString *urlstr = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php";
//  //  NSString *urlEncode = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
//    NSString *urlstr = [AFH stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
//     NSURL *url = [NSURL URLWithString:urlstr];
//    
//    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//     NSURLSession *session = [NSURLSession sharedSession];
//  NSURLSessionDataTask *task   = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
//        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
//      //  NSLog(@"result === %@", result);
//      appBlock(result);
//
//    }];
    
    
    NSString *urlEncode = [AFH stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:urlEncode parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSLog(@"使用AFN进行get请求 ===  %@",responseObject);//字典。数组
        //block回调
        appBlock(responseObject);
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
        
    }];

    
    
//    //开始请求数据
//    [task resume];
    
        


}
+ (void)getLocalDataWithimage:(NSString *)image andBlock:(ApptoolsBlock)appBlock{
    NSURL *url = [NSURL URLWithString:image];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task   = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        UIImage *image =[UIImage imageWithData:data];
    //    NSLog(@"result === %@", image);
        appBlock(image);
        
    }];
    //开始请求数据
    [task resume];
    
    

}
@end
